<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Menu extends MController {

	var $mid = null;

	function __construct ()
	{
		parent::__construct();
		$this->load->model('Menumodel');
		$this->load->helper('form');
		$this->load->helper('url');
		$this->lang->load('menu', 'Russian');
	}

	function index ()
	{
		return parent::display($this->lang->line('menu_wellcome'));
	}

	/**
	 * User configuration page.
	 */
//	function configuration ($menu_id = null) {
//		if ($menu_id) { $this->Menumodel->mid = $menu_id; }
//		$this->Menumodel->get();
//		$data = array('menu' => $this->Menumodel, 'configs' => $this->getConfigs());
//		$content = $this->load->view('menu_config', $data, true);
//		parent::display($data);
//	}
	
	function getConfigs ($menu_id = null) {
		$this->load->model('Configurationmodel');
		
		if (empty($menu_id)) { $menu_id = $this->Menumodel->mid; }
		
		$this->Configurationmodel->object_id = $menu_id;
		$this->Configurationmodel->group = 'menu';
		
//		TODO CHECK AND FIX THIS!
//		var_dump($this->Configurationmodel->get());
//		die;
		
		return $this->Configurationmodel->get();
	}
	
	function edit ($menu_id = null) {
		$this->mid = $this->Menumodel->mid = $menu_id;
		$sent = $this->input->post('submit');
		$form = $this->getMenuForm(!$sent);
		if ($sent) {
			
			$errorFlag = false;
			$error = array();
			$inputs = array();
			$dependsArray = array(0 => array(), 1 => array());
			
			foreach ($form['inputs'] as $key => $input) {
				if (!$this->input->post($key)) {
					
					if (!empty($dependsArray[0][$key])) {
						$errorFlag = true;
						$error[] = $key;
						$error = array_merge($error, $dependsArray[0][$key]);
					}
					
					if ($input['required']) {
						$errorFlag = true;
						$error[] = $key;
					} elseif (!empty($input['required_if'])) {
						if (!is_array($input['required_if'])) { $input['required_if'] = array($input['required_if'] => false); }
						foreach ($input['required_if'] as $field => $filled) {
							if (array_key_exists($field, $inputs) && ($filled !== (bool)$inputs[$field])) {
								$errorFlag = true;
								die($field);
								$error[] = $key;
							} elseif(!array_key_exists($field, $inputs)) { 
								$dependsArray[(int)$filled][$field][] = $key;
							}
						}
					}
				} elseif (!empty($dependsArray[1][$key])) {
					$errorFlag = true;
					$error[] = $key;
					$error = array_merge($error, $dependsArray[1][$key]);
				}
				
				$inputs[$key] = $this->Configurationmodel->$key = $this->input->post($key);
			}
			
			if ($inputs['new_section']) { $this->Configurationmodel->section = $inputs['new_section']; }
			
			if ($errorFlag) {
				$this->setMessage($this->lang->line('configuration_error_filling_form'), 'error');
				$this->setMessage(array('error' => $error, 'inputs' => $inputs), 'form');
				redirect('configuration/add');
				return ;
			}
			
			if (empty($inputs['cid'])) { $inputs['order'] = $this->Configurationmodel->order = $this->Configurationmodel->checkOrder(); }

			if ($this->Configurationmodel->save()) {
				$this->setMessage($this->lang->line('configuration_save_success'), 'Success');
			} else {
				$this->setMessage($this->lang->line('configuration_save_failed'), 'Error');
			}
			redirect('/menu');
		} else {
			$this->load->helper('form');

			if ($config_id) {
				$this->Configurationmodel->cid = $config_id;
				$this->Configurationmodel->get();
			}
			
			$form = $this->getMenuForm();
			var_dump($form);
			$content = $this->load->view('admin_config_form', $form, true);
		}
		parent::display($content);
	}
	
	function getMenuForm ($render = true) {
		
		$this->load->model('Configurationmodel');
		$this->load->library('MConfiguration');
		
		$form = array(
				'inputs' => array(
					'name' => array('required' => true),
					'comment' => array('required' => true),
					'style' => array('required' => false),
					'order' => array('required' => false)));
		
		$configs_form = $this->mconfiguration->form('menu', $this->Menumodel->mid, $render);
		
		$form = array_merge_recursive($form, $configs_form);
		
		if ($render) {
			
			if (array_key_exists('form', $this->messages)) {
				$errors = $this->messages['form']['error'];
				$inputs = $this->messages['form']['inputs'];
			} else {
				$errors = array();
				$inputs = array();
			}
			
			$form['open_tag'] = form_open($this->uri->uri_string());
			$form['close_tag'] = form_close();
			
			$form['controls'] = array(
				'submit' => form_submit('submit', $this->lang->line('configuration_save_label')), 
				'cancel' => form_submit('cancel', $this->lang->line('configuration_cancel_label')));
		}
		return $form;
	}
}

/* End of file menu.php */
/* Location: ./system/application/controllers/menu.php */
